/*
 * Copyright 2008 the Breachwalls OSS project and the Others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package com.breachwalls.mogen.utils;

import java.io.File;
import java.net.URL;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

/**
 * Dom4jに関するユーティリティ.
 * @author todoken
 * @version $Revision: 31 $
 */
public final class Dom4jUtils {

    /**
     * プライベートコンストラクタ.
     */
    private Dom4jUtils() {
    }

    /**
     * URLを元にDom4jドキュメントを生成する.
     * @param urls XMLドキュメントのURLオブジェクト
     * @return Dom4jで使用可能なドキュメントオブジェクト
     */
    public static Document[] createDocument(final URL... urls) {
        SAXReader reader = new SAXReader();
        Document[] documents = new Document[urls.length];
        for (int i = 0; i < urls.length; i++) {
            File file = new File(urls[i].getFile());
            try {
                documents[i] = reader.read(file);
            } catch (DocumentException e) {
                throw new RuntimeException(e);
            }
        }
        return documents;
    }

    /**
     * ファイルを元にDom4jドキュメントを生成する
     * @param files XMLドキュメントのファイルオブジェクト
     * @return Dom4jで使用可能なドキュメントオブジェクト
     */
    public static Document[] createDocument(final File... files) {
        SAXReader reader = new SAXReader();
        Document[] documents = new Document[files.length];
        for (int i = 0; i < files.length; i++) {
            try {
                documents[i] = reader.read(files[i]);
            } catch (DocumentException e) {
                throw new RuntimeException(e);
            }
        }
        return documents;
    }

}
